//
//  SearchEmploymentVC.m
//  CenturyCity
//
//  Created by beisheng03 on 2018/1/15.
//  Copyright © 2018年 beisheng. All rights reserved.
//

#import "SearchEmploymentVC.h"
#import "EmploymentListCell.h"
#import "EmploymentListTCell.h"
#import "EmploymentDetailVC.h"
#import "EmploymentSearch.h"
#import "EmploymentListModel.h"
#import "EmploymentNothingCell.h"

@interface SearchEmploymentVC ()
@property (nonatomic, assign) NSInteger curpage;

@property (nonatomic, assign) NSString *pageCount;

@property (nonatomic, assign) NSString *totalCount;

@property (nonatomic, strong) NSMutableArray<EmploymentListModel *> *dataModels;

@end
@implementation SearchEmploymentVC

- (void)viewDidLoad {
    [super viewDidLoad];
    

    
    
    if (self.navTitle.length > 0) {
        self.navigationItem.title = self.navTitle;
    } else {
        self.navigationItem.title = @"招聘信息";
    }
        
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.plainTableView.backgroundColor = viewBgGrey;
    
    self.plainTableView.rowHeight = 96;
    
    [self.plainTableView registerNib:[UINib nibWithNibName:NSStringFromClass([EmploymentListCell class]) bundle:nil] forCellReuseIdentifier:[EmploymentListCell identifierStr]];
    
    [self.plainTableView registerNib:[UINib nibWithNibName:NSStringFromClass([EmploymentListTCell class]) bundle:nil] forCellReuseIdentifier:[EmploymentListTCell identifierStr]];
    
    [self.plainTableView registerNib:[UINib nibWithNibName:NSStringFromClass([EmploymentSearch class]) bundle:nil] forHeaderFooterViewReuseIdentifier:[EmploymentSearch identifierStr]];
    
    [self.plainTableView registerNib:[UINib nibWithNibName:NSStringFromClass([EmploymentNothingCell class]) bundle:nil] forCellReuseIdentifier:[EmploymentNothingCell identifierStr]];
    
    
    MJWeakSelf
    self.plainTableView.mj_header = [XMGRefreshHeader headerWithRefreshingBlock:^{
        
        [weakSelf loadData];
        
    }];
    
    [weakSelf.plainTableView.mj_header beginRefreshing];
    
    
    
    
    
    
    
    
    //    CGFloat navH = IPhoneX ? 88 : 64;
    //    CGFloat tabbarH = IPhoneX ? 83 : 49;
    //
    //    if (@available(iOS 11.0, *)) {
    //
    //        self.plainTableView.contentInset = UIEdgeInsetsMake(XMGTitlesViewH, 0, tabbarH, 0);
    //
    //    }else{
    //
    //        self.plainTableView.contentInset = UIEdgeInsetsMake(navH + XMGTitlesViewH, 0, tabbarH,0 );
    //
    //
    //    }
    
    // Do any additional setup after loading the view.
}



- (void)loadData{
    self.curpage = 1;
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    
    parameters[@"page"] = @"1";
    
    parameters[@"pagesize"] = @"20";
    
    if (self.keyword != nil) {
        parameters[@"keyword"] = self.keyword;

    } else {
        parameters[@"supplierId"] = self.supplierID;

    }

    
    MJWeakSelf;
    [[NetworkTools shareNetworkTools]sendRequestWithHttpMethod:GET_METHOD URLPath:@"Recruit/getRecruitLists" parameters:parameters completionHandler:^(id responseObject, NSError *error) {
        
        if (!error) {
            
            //                        MYJSONSTR
            
            //                       [[responseObject[@"data"][@"data"] firstObject]  propertyCode];
            
            weakSelf.pageCount = responseObject[@"data"][@"page_count"];
            
            weakSelf.totalCount = responseObject[@"data"][@"total_count"];
            
            
            weakSelf.dataModels = [EmploymentListModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"data"]];
            
            //
            //            [weakSelf.dataModels removeAllObjects];
            //
            //
            //            weakSelf.totalCount = @"0";
            //
            //            NSLog(@"%ld",weakSelf.dataModels.count);
            
            if ([weakSelf.totalCount isEqualToString:@"0"]) {
                
                
                
            } else {
                
                weakSelf.plainTableView.mj_footer = [XMGRefreshFooter footerWithRefreshingBlock:^{
                    
                    [weakSelf loadMoreData];
                    
                }];
                
                [weakSelf.plainTableView.mj_footer resetNoMoreData];
                
                
                if ([weakSelf.pageCount isEqualToString:@"1"]) {
                    
                    [weakSelf.plainTableView.mj_footer endRefreshingWithNoMoreData];
                    
                }
            }
            
            [weakSelf.plainTableView.mj_header endRefreshing];
            
            
            
            [weakSelf.plainTableView reloadData];
            
            
        } else {
            
            [weakSelf.plainTableView.mj_header endRefreshing];
            
            [SVProgressHUD showInfoWithStatus:error.domain];
            
        }
    }];
    
    
    
}



- (void)loadMoreData{
    
    self.curpage ++;
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"pagesize"] = @"20";
    parameters[@"page"] = [NSString stringWithFormat:@"%ld",(long)self.curpage];
    
    if (self.keyword != nil) {
        parameters[@"keyword"] = self.keyword;
        
    } else {
        parameters[@"supplierId"] = self.supplierID;
        
    }

    
    MJWeakSelf;
    [[NetworkTools shareNetworkTools] sendRequestWithHttpMethod:GET_METHOD URLPath:@"Recruit/getRecruitLists" parameters:parameters completionHandler:^(id responseObject, NSError *error) {
        
        if (!error) {
            
            NSMutableArray<EmploymentListModel *> *temp = [EmploymentListModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"data"]];
            
            
            
            
            [weakSelf.dataModels addObjectsFromArray:temp];
            
            
            
            
            if (weakSelf.pageCount.integerValue == self.curpage) {
                
                [weakSelf.plainTableView.mj_footer endRefreshingWithNoMoreData];
                
            } else {
                
                [weakSelf.plainTableView.mj_footer endRefreshing];
                
                
            }
            
            
            [weakSelf.plainTableView reloadData];
            
        }else{
            
            [weakSelf.plainTableView.mj_footer endRefreshing];
            
            [SVProgressHUD showInfoWithStatus:error.domain];
            
        }
        
    }];
    
    
    
}



#pragma mark - tableviewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    if (self.dataModels == nil) {
        return  0;
    } else {
        return  1;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.dataModels == nil) {
        return  0;
    } else {
        
        if (self.dataModels.count == 0) {
            return 1;
        } else {
            return  self.dataModels.count * 2;
        }
        
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (self.dataModels.count == 0) {
        
        EmploymentNothingCell *tableCell = [tableView dequeueReusableCellWithIdentifier:[EmploymentNothingCell identifierStr]];
        
        
        return tableCell;
        
    } else {
        
        
        if (indexPath.row % 2 == 0) {
            EmploymentListCell *tableCell = [tableView dequeueReusableCellWithIdentifier:[EmploymentListCell identifierStr]];
            
            tableCell.model = self.dataModels[indexPath.row/2];
            
            return tableCell;
            
        } else {
            
            EmploymentListTCell *tableCell = [tableView dequeueReusableCellWithIdentifier:[EmploymentListTCell identifierStr]];
            
            if (indexPath.row == ((self.dataModels.count * 2) - 1)) {
                
                tableCell.isLastCell = YES;
                
            } else {
                
                tableCell.isLastCell = NO;

            }
            
            
            tableCell.model = self.dataModels[(indexPath.row - 1) / 2];
            
      
            return tableCell;
        }
        
        
    }
    
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (self.dataModels.count == 0) {
        CGFloat tempH = IPhoneX ? 88 : 64;
        return KScreenH - tempH;
    } else {
        
        if (indexPath.row % 2 == 0) {
            return 72;
        } else {
            return 52;
        }
        
    }
    
}


//
//- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
//    
//    return 0.00001;
//}
//
//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
//    
//    if (self.dataModels == nil || self.dataModels.count == 0) {
//        
//        return 0.00001;
//        
//        
//    }else{
//        
//        return 52;
//    }
//    
//    
//    
//    
//    
//    
//}
//
//
//
//- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
//    
//    
//    UIView *temp = [[UIView alloc]init];
//    temp.backgroundColor = [UIColor whiteColor];
//    return temp;
//}
//
//
//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
//    
//    if (self.dataModels == nil || self.dataModels.count == 0) {
//        
//        UIView *temp = [[UIView alloc]init];
//        temp.backgroundColor = [UIColor whiteColor];
//        return temp;
//        
//    }else{
//        
//        EmploymentSearch *view = (EmploymentSearch *)[tableView dequeueReusableHeaderFooterViewWithIdentifier:[EmploymentSearch identifierStr]];
//        
//        view.type = @"2";
//        
//        return view;
//    }
//    
//    
//    
//}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    EmploymentListModel *tempModel;
    if (indexPath.row % 2 == 0) {
        
        tempModel = self.dataModels[indexPath.row/2];
        
    } else {
        
        tempModel = self.dataModels[(indexPath.row - 1) / 2];
        
    }
    
    
    EmploymentDetailVC *tempVC = [[EmploymentDetailVC alloc]init];
    
    tempVC.IdStr = tempModel.ID;
    
    [self.navigationController pushViewController:tempVC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
